//
//  TravelViewController.m
//  Community
//
//  Created by YaSha_Tom on 2018/12/21.
//  Copyright © 2018年 YaSha-Tom. All rights reserved.
//

#import "TravelViewController.h"
#import "TravelTableViewCell.h"
#import "TravelInfoViewController.h"
#import <ReactiveCocoa.h>
#import <BmobSDK/Bmob.h>
#import "OutputView.h"
#import "UIImage+LX.h"

@interface TravelViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation TravelViewController
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"动态";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
    [self doNetworking];
    [self setupNavBar];
}
- (void)setupNavBar {
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage getImageWithColor:[UIColor qmui_colorWithHexString:@"E7494D"]] forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)initUI {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[TravelTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell.moreButton setImage:[UIImage imageNamed:@"find_more_btn"] forState:UIControlStateNormal];
    [cell.moreButton addTarget:self action:@selector(moreBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.moreButton.tag = indexPath.row;
    BmobObject *obj = self.dataSource[indexPath.row];
    [cell setTravelData:obj];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TravelInfoViewController *travelInfoViewController = [[TravelInfoViewController alloc]init];
    travelInfoViewController.obj = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:travelInfoViewController animated:YES];
}
//更多
-(void)moreBtn:(UIButton*)button{
    UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
    CGRect rect = [button convertRect:[button bounds] toView:window];
    Direction direction = OutputViewDirectionRight;
    CGFloat origin_y = rect.origin.y+rect.size.height;
    if (rect.origin.y+rect.size.height/2>(Height-64)/2) {
        direction = OutputViewDirectionDownRight;
        origin_y = rect.origin.y;
    }
    NSArray *dataArray = @[@{@"title":@"关注",@"img":@"follow_icon"},
                           @{@"title":@"收藏",@"img":@"collection_icon"},
                           @{@"title":@"隐藏此条动态",@"img":@"hide_dynamic_icon"},
                           @{@"title":@"不看他的动态",@"img":@"not_look_icon"}];
    OutputView *outputV = [[OutputView alloc] initWithPoint:CGPointMake(Width, origin_y) rowHeigth:50*rateh direction:direction dataArray:dataArray];
    [outputV setRowWidth:Width];//重新设置弹框宽
    [outputV setArrow_x:11*rateh];//设置箭头位置
    outputV.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundView_alpha"]];
    outputV.tableView.backgroundColor = [UIColor whiteColor];
    outputV.celltextColor = [UIColor blackColor];
    outputV.textAlignment = NSTextAlignmentLeft;
    outputV.tableView.layer.cornerRadius = 0;
    [outputV popForTwo];
    outputV.resultBlock = ^(id result){
        NSLog(@"result:%@",result);
        //此处是更新操作
        BmobObject *obj = self.dataSource[button.tag];
        if ([result isEqualToString:@"3"]) {
            BmobQuery   *bquery = [BmobQuery queryWithClassName:@"comments"];
            //添加playerName不是小明的约束条件
            [bquery whereKey:@"name" equalTo:[obj objectForKey:@"name"]];
            [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                for (BmobObject *obj in array) {
                    //打印playerName
                    BmobObject  *gameScoreChange = [BmobObject objectWithoutDataWithClassName:@"comments" objectId:[obj objectForKey:@"objectId"]];
                        [gameScoreChange setObject:[NSNumber numberWithBool:YES] forKey:@"hidden"];
                    @weakify(self);
                    [gameScoreChange updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        @strongify(self);
                        if (isSuccessful) {
                            NSLog(@"更新成功，以下为对象值，可以看到score值已经改变");
                            [self doNetworking];
                        } else {
                            NSLog(@"%@",error);
                        }
                    }];
                }
            }];
        }else{
            BmobObject  *gameScoreChange = [BmobObject objectWithoutDataWithClassName:@"comments" objectId:[obj objectForKey:@"objectId"]];
            if ([result isEqualToString:@"0"]) {
                //关注
                [gameScoreChange setObject:[NSNumber numberWithBool:YES] forKey:@"attention"];
            }else if([result isEqualToString:@"1"]){
                [gameScoreChange setObject:[NSNumber numberWithBool:YES] forKey:@"collection"];
            }
            else if([result isEqualToString:@"2"]){
                [gameScoreChange setObject:[NSNumber numberWithBool:YES] forKey:@"hidden"];
            }
            @weakify(self);
            [gameScoreChange updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                @strongify(self);
                if (isSuccessful) {
                    NSLog(@"更新成功，以下为对象值，可以看到score值已经改变");
                    [self doNetworking];
                } else {
                    NSLog(@"%@",error);
                }
            }];
        }
    };
}

- (void)doNetworking {
    BmobQuery  *bquery = [BmobQuery queryWithClassName:@"comments"];
    //查找GameScore表的数据
    @weakify(self);
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        @strongify(self);
        [self.dataSource removeAllObjects];
        for (BmobObject *obj in array) {
            NSLog(@"----%@",[obj objectForKey:@"hidden"]);
            if ([[obj objectForKey:@"hidden"]  isEqual: @0] ) {
                [self.dataSource addObject:obj];
            }
        }
        NSLog(@"------%lu",(unsigned long)self.dataSource.count);
        [self.tableView reloadData];
    }];
}
 
@end
